using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaFavoriteInfo : 
    ReadOnlyBase<CslaFavoriteInfo>,
    IFavorite
  {
    #region [ Business Properties ]

    public static PropertyInfo<double?> VotePropertyInfo = RegisterProperty(new PropertyInfo<double?>("Vote"));
    public double? Vote 
    { 
      get
      {
        return ReadProperty(VotePropertyInfo);
      }
      private set
      {
        LoadProperty(VotePropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserInfo> UserPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserInfo>("User"));
    public CslaUserInfo User 
    { 
      get
      {
        return ReadProperty(UserPropertyInfo);
      }
      set
      {
        LoadProperty(UserPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectInfo> SubjectPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectInfo>("Subject"));
    public CslaSubjectInfo Subject 
    { 
      get
      {
        return ReadProperty(SubjectPropertyInfo);
      }
      set
      {
        LoadProperty(SubjectPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaFavoriteDerivedInfoCollection> SuperFavoritesPropertyInfo = RegisterProperty(new PropertyInfo<CslaFavoriteDerivedInfoCollection>("SuperFavorites"));
    public CslaFavoriteDerivedInfoCollection SuperFavorites 
    { 
      get
      {
        return ReadProperty(SuperFavoritesPropertyInfo);
      }
      private set
      {
        LoadProperty(SuperFavoritesPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaFavoriteInfo GetFavoriteInfo(Favorite data, FavoriteLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaFavoriteInfo result;
      if (data is FavoriteDerived)
        result = CslaFavoriteDerivedInfo.GetFavoriteDerivedInfo((FavoriteDerived)data, FavoriteDerivedLoadSettings.FromFavoriteLoadSettings(loadSettings));
      else
      {
        result = new CslaFavoriteInfo();
        result.FetchFavorite(data, loadSettings);
      }
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaFavoriteInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchFavorite(Favorite data, FavoriteLoadSettings loadSettings)
    {
      FetchFavoriteData(data, loadSettings);
      FetchFavoriteChildren(data, loadSettings);
    }

    protected void FetchFavoriteData(Favorite data, FavoriteLoadSettings loadSettings)
    {
      this.Vote = data.Vote;
    }

    protected void FetchFavoriteChildren(Favorite data, FavoriteLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeSuperFavorites)
        this.SuperFavorites = CslaFavoriteDerivedInfoCollection.GetFavoriteDerivedInfoCollection(data.SuperFavorites, loadSettings.Inclusion.IncludedSuperFavoritesSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IFavorite implementation ]

    double? IFavorite.Vote
    {
      get { return this.Vote; }
      set
      {
        throw new NotSupportedException();
      }
    }

    IUser IFavorite.User
    { 
      get
      {
        return this.User;
      }
      set
      {
        throw new NotSupportedException();
      }
    }

    ISubject IFavorite.Subject
    { 
      get
      {
        return this.Subject;
      }
      set
      {
        throw new NotSupportedException();
      }
    }

    IBusinessObjectCollection<IFavoriteDerived> IFavorite.SuperFavorites
    { 
      get
      {
        return this.SuperFavorites;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit IFavorite implementation ]
  }
}
